<template>
  <div class="login-container">
    <div class="login-box">
      <!--表单-->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        class="login-form"
        :rules="rules"
      >
        <el-form-item prop="loginName">
          <el-input
            v-model="loginForm.loginName"
            :prefix-icon="User"
            placeholder="lcx"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            :prefix-icon="Lock"
            show-password
            placeholder="lcx123"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="btns">
            <el-button
              type="danger"
              @click="login(loginFormRef)"
              plain
              :loading="loading"
              >登录</el-button
            >
            <el-button type="info" @click="resetForm(loginFormRef)" plain
              >重置</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import "element-plus/dist/index.css"; // 引入组件样式
import { ElFormItem, ElButton, ElInput, ElForm } from "element-plus";
import { User, Lock, Key } from "@element-plus/icons-vue";
import type { FormInstance, FormRules } from "element-plus";
import { message } from "ant-design-vue";
import { userLogin } from "@/api/user";
import { getToken, setToken } from "@/util/auth";
import { hexMd5, KEY } from "@/util/md5";

const router = useRouter();
let a = useRoute();

// 登录表单数据
const loginForm = ref({
  loginName: "lcx",
  password: "lcx1234",
});
const rules = reactive<FormRules>({
  loginName: [
    { required: true, message: "请输入用户名", trigger: "blur" }, //blur表示失去焦点的时候触发
    { min: 3, max: 10, message: "长度需要在3到10之间", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    // { min: 6, max: 15, message: "长度需要在6到15之间", trigger: "blur" },
  ],
});
const loading = ref<boolean>(false);
const loginFormRef = ref<FormInstance>();
const login = async (loginFormRef: FormInstance | undefined) => {
  if (!loginFormRef) return;
  await loginFormRef.validate((valid, fields) => {
    if (valid) {
      // 加密
      loginForm.value.password = hexMd5(loginForm.value.password + KEY);
      console.log(loginForm.value.password);

      loading.value = true;
      userLogin(loginForm.value)
        .then((res) => {
          loading.value = false;
          // console.log(res);
          if (res.data.code === 10001) {
            message.success("登录成功！");
            // 存入cookie
            setToken(res.data.data.token, res.data.data.name);

            setTimeout(() => {
              router.push("/home");
              router.push("/home");
            }, 1000);
          } else {
            message.error(res.data.msg);
          }
        })
        .catch((err) => {
          // 超时？
          loading.value = false;
          message.error(err.message);
        });
    } else {
      message.warn("请正确填写");
    }
  });
};
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
if (getToken()) {
  //存在token了 就不需要进入登录页面了
  router.push("/home");
}
</script>

<style scoped>
.login-container {
  position: relative;
  height: 100%;
  background-color: #e5e9edef;
}

.login-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  border-radius: 20px;
  box-shadow: 0 0 10px gray;
}

.login-form {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  padding: 0 30px;
  box-sizing: border-box;
}
:deep(.el-input__inner) {
  height: 40px;
}
:deep(.el-input__wrapper) {
  margin: 8px;
}

.el-form-item__content {
  position: relative;
  display: flex;
}
.btns {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translate(-50%, 0);
}

.el-button {
  width: 74px;
  height: 38px;
}
.el-button:nth-child(1) {
  margin-right: 30px;
}
</style>
